<template>
  <div>
    <el-form-item label="选择序列号">
      <el-select v-model="localData.sequenceCode" @change="handleSequenceChange" clearable>
        <el-option v-for="opt in sequenceList" :label="opt.sequenceName" :value="opt.sequenceCode" :key="opt.sequenceCode" />
      </el-select>
    </el-form-item>
    <el-form-item label="生成前钩子">
      <el-select v-model="localData.hookBefore" clearable>
        <el-option v-for="opt in getPageFuncs()" :key="opt.funcName" :label="opt.funcLabel" :value="opt.funcName" />
      </el-select>
    </el-form-item>
    <el-form-item label="生成后钩子">
      <el-select v-model="localData.hookAfter" clearable>
        <el-option v-for="opt in getPageFuncs()" :key="opt.funcName" :label="opt.funcLabel" :value="opt.funcName" />
      </el-select>
    </el-form-item>
  </div>
</template>

<script>
import { searchSequence } from '@code/components/anvil-code-low-common/api/uni';
export default {
  name: 'SerialNumberConfig',
  props: {
    data: {
      type: Object,
      default: () => {}
    },
    formJson: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      localData: {},
      sequenceList: []
    };
  },
  watch: {
    data: {
      handler(newVal) {
        if (newVal) {
          this.localData = newVal;
        }
      },
      deep: true,
      immediate: true
    },
    localData: {
      handler(newVal) {
        this.$emit('update:data', newVal);
      },
      deep: true
    }
  },
  mounted() {
    this.getSequence();
  },
  methods: {
    getSequence() {
      return searchSequence().then((response) => {
        this.sequenceList = response.data;
      });
    },
    handleSequenceChange(value) {
      const sequence = this.sequenceList.find((item) => item.sequenceCode === value);
      if (sequence) {
        const { sequenceFormat } = sequence;
        this.localData.placeholder = sequenceFormat;
      } else {
        this.localData.placeholder = '';
      }
    },
    getPageFuncs() {
      return this.formJson.funcs ?? [];
    }
  }
};
</script>

<style scoped></style>
